package com.dianping.cat.consumer.cross.model.transform;

import com.dianping.cat.consumer.cross.model.IVisitor;
import com.dianping.cat.consumer.cross.model.entity.CrossReport;
import com.dianping.cat.consumer.cross.model.entity.Local;
import com.dianping.cat.consumer.cross.model.entity.Name;
import com.dianping.cat.consumer.cross.model.entity.Remote;
import com.dianping.cat.consumer.cross.model.entity.Type;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

/*  22 */   public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
/*  26 */     this.m_out = new DataOutputStream(out);
/*  27 */     this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(CrossReport crossReport) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

/*  33 */     build(crossReport, out);
/*  34 */     return out.toByteArray();
  }


/*  38 */   public static void build(CrossReport crossReport, OutputStream out) { crossReport.accept(new DefaultNativeBuilder(out)); }



  public void visitCrossReport(CrossReport crossReport) {
/*  43 */     writeTag(63, 0);

/*  45 */     if (crossReport.getDomain() != null) {
/*  46 */       writeTag(1, 1);
/*  47 */       writeString(crossReport.getDomain());
    }

/*  50 */     if (crossReport.getStartTime() != null) {
/*  51 */       writeTag(2, 1);
/*  52 */       writeDate(crossReport.getStartTime());
    }

/*  55 */     if (crossReport.getEndTime() != null) {
/*  56 */       writeTag(3, 1);
/*  57 */       writeDate(crossReport.getEndTime());
    }

/*  60 */     if (crossReport.getDomainNames() != null) {
/*  61 */       writeTag(4, 2);
/*  62 */       writeInt(crossReport.getDomainNames().size());

/*  64 */       for (String domain : crossReport.getDomainNames()) {
/*  65 */         writeString(domain);
      }
    }

/*  69 */     if (crossReport.getIps() != null) {
/*  70 */       writeTag(5, 2);
/*  71 */       writeInt(crossReport.getIps().size());

/*  73 */       for (String ip : crossReport.getIps()) {
/*  74 */         writeString(ip);
      }
    }

    if (!crossReport.getLocals().isEmpty()) {
/*  79 */       writeTag(33, 2);
/*  80 */       writeInt(crossReport.getLocals().size());

/*  82 */       for (Local local : crossReport.getLocals().values()) {
/*  83 */         local.accept(this.m_visitor);
      }
    }

/*  87 */     writeTag(63, 3);
  }


  public void visitLocal(Local local) {
/*  92 */     if (local.getId() != null) {
/*  93 */       writeTag(1, 1);
/*  94 */       writeString(local.getId());
    }

/*  97 */     if (!local.getRemotes().isEmpty()) {
/*  98 */       writeTag(33, 2);
/*  99 */       writeInt(local.getRemotes().size());

/* 101 */       for (Remote remote : local.getRemotes().values()) {
/* 102 */         remote.accept(this.m_visitor);
      }
    }

/* 106 */     writeTag(63, 3);
  }


  public void visitName(Name name) {
/* 111 */     if (name.getId() != null) {
/* 112 */       writeTag(1, 1);
/* 113 */       writeString(name.getId());
    }

/* 116 */     writeTag(2, 0);
/* 117 */     writeLong(name.getTotalCount());

/* 119 */     writeTag(3, 0);
/* 120 */     writeInt(name.getFailCount());

/* 122 */     writeTag(4, 0);
/* 123 */     writeDouble(name.getFailPercent());

/* 125 */     writeTag(5, 0);
/* 126 */     writeDouble(name.getAvg());

/* 128 */     writeTag(6, 0);
/* 129 */     writeDouble(name.getSum());

/* 131 */     writeTag(7, 0);
/* 132 */     writeDouble(name.getTps());

/* 134 */     writeTag(63, 3);
  }


  public void visitRemote(Remote remote) {
/* 139 */     if (remote.getId() != null) {
/* 140 */       writeTag(1, 1);
/* 141 */       writeString(remote.getId());
    }

/* 144 */     if (remote.getRole() != null) {
/* 145 */       writeTag(2, 1);
/* 146 */       writeString(remote.getRole());
    }

/* 149 */     if (remote.getApp() != null) {
/* 150 */       writeTag(3, 1);
/* 151 */       writeString(remote.getApp());
    }

/* 154 */     if (remote.getIp() != null) {
/* 155 */       writeTag(4, 1);
/* 156 */       writeString(remote.getIp());
    }

/* 159 */     if (remote.getType() != null) {
/* 160 */       writeTag(33, 1);
/* 161 */       remote.getType().accept(this.m_visitor);
    }

/* 164 */     writeTag(63, 3);
  }


  public void visitType(Type type) {
/* 169 */     if (type.getId() != null) {
/* 170 */       writeTag(1, 1);
/* 171 */       writeString(type.getId());
    }

/* 174 */     writeTag(2, 0);
/* 175 */     writeLong(type.getTotalCount());

/* 177 */     writeTag(3, 0);
/* 178 */     writeInt(type.getFailCount());

/* 180 */     writeTag(4, 0);
/* 181 */     writeDouble(type.getFailPercent());

/* 183 */     writeTag(5, 0);
/* 184 */     writeDouble(type.getAvg());

/* 186 */     writeTag(6, 0);
/* 187 */     writeDouble(type.getSum());

/* 189 */     writeTag(7, 0);
/* 190 */     writeDouble(type.getTps());

/* 192 */     if (!type.getNames().isEmpty()) {
/* 193 */       writeTag(33, 2);
/* 194 */       writeInt(type.getNames().size());

/* 196 */       for (Name name : type.getNames().values()) {
/* 197 */         name.accept(this.m_visitor);
      }
    }

/* 201 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 206 */       writeVarint(value.getTime());
/* 207 */     } catch (Exception e) {
/* 208 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 214 */       writeVarint(Double.doubleToLongBits(value));
/* 215 */     } catch (Exception e) {
/* 216 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 222 */       writeVarint(value & 0xFFFFFFFFL);
/* 223 */     } catch (Exception e) {
/* 224 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 230 */       writeVarint(value);
/* 231 */     } catch (Exception e) {
/* 232 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 238 */       this.m_out.writeUTF(value);
/* 239 */     } catch (IOException e) {
/* 240 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 246 */       this.m_out.writeByte((field << 2) + type);
/* 247 */     } catch (IOException e) {
/* 248 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 254 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 255 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 258 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 259 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/cross/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */